var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
var concat = require('gulp-concat');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sh = require('shelljs');
var uglify = require('gulp-uglify');
var useref = require('gulp-useref');
var gulpif = require('gulp-if');
var clean = require('gulp-clean');
var gulpSequence = require('gulp-sequence');
var jshint = require('gulp-jshint');
var map = require('map-stream');
var templateCache = require('gulp-angular-templatecache');
var minifyHtml = require('gulp-minify-html');

var paths = {
    sass: ['./scss/**/*.scss', './scss/all.scss']
};

gulp.task('clean', function(done) {
    gulp.src('www/dist')
        .pipe(clean());
});

var myReporter;
myReporter = map(function(file, cb) {
    console.log('file, cb', file, cb)
    if (!file.jshint.success) {
        console.log('JSHINT fail in ' + file.path);
        file.jshint.results.forEach(function(err) {
            if (err) {
                console.log(err.error.line);
            }
        });
    }
    cb(null, file);
});
gulp.task('lint', function() {
    gulp.src('./www/js/**/*.js').pipe(jshint()).pipe(myReporter);
});

gulp.task('sass', function(done) {

    gulp.src(['./scss/ionic.app.scss', './scss/all.scss'])
        .pipe(sass())
        .on('error', sass.logError)
        .pipe(gulp.dest('./www/css/'))
        .pipe(minifyCss({
            keepSpecialComments: 0
        }))
        .pipe(rename({ extname: '.min.css' }))
        .pipe(gulp.dest('./www/css/'))
        .on('end', done);
});

gulp.task('scss', function(done) {
    gulp.src(['./scss/all.scss'])
        .pipe(sass())
        .on('error', sass.logError)
        .pipe(gulp.dest('./www/css/'))
        .pipe(minifyCss({
            keepSpecialComments: 0
        }))
        .pipe(rename({ extname: '.min.css' }))
        .pipe(gulp.dest('./www/css/'))
        .on('end', done);
});

gulp.task('watch', function() {
    gulp.watch(paths.sass, ['sass']);
    gulp.watch('./scss/all.scss', ['scss']);
});

gulp.task('templatecache', function(done) {
    gulp.src('./www/templates/**/*.html')
        .pipe(minifyHtml())
        .pipe(templateCache({
            standalone: true,
            module: 'templatescache',
            root: 'templates'
        }))
        .pipe(gulp.dest('./www/js'))
        .on('end', done);
});

var highcharts = [
    './www/dist/js/verder.min.js',
    './www/lib/highcharts/highcharts.js',
    './www/lib/highcharts/highstock.js',
    './www/lib/highcharts/highcharts-3d.js',
    './www/lib/highcharts/highcharts-more.js'
];

gulp.task('copy', function(done) {
    gulp.src(highcharts)
        .pipe(gulp.dest('./www/dist/lib/highcharts'));
    gulp.src(['www/lib/highcharts/modules/exporting.js'])
        .pipe(gulp.dest('./www/dist/lib/highcharts/modules'));
    gulp.src(['www/lib/highcharts/themes/grid-light.js'])
        .pipe(gulp.dest('./www/dist/lib/highcharts/themes'));

    gulp.src(['./www/img/**/*'])
        .pipe(gulp.dest('./www/dist/img'));
    gulp.src(['./www/css/fonts/*'])
        .pipe(gulp.dest('./www/dist/css/fonts'));
    gulp.src(['./www/lib/ionic/fonts/*'])
        .pipe(gulp.dest('./www/dist/fonts'));
});

gulp.task('useref', function(done) {
    gulp.src('./www/index.html')
        .pipe(useref())
        .pipe(gulpif('*.js', uglify()))
        .pipe(gulpif('*.css', minifyCss()))
        .pipe(gulp.dest('./www/dist'))
        .on('end', done);
});

gulp.task('default', gulpSequence('sass', 'templatecache', 'useref', 'copy'));

gulp.task('install', ['git-check'], function() {
    return bower.commands.install()
        .on('log', function(data) {
            gutil.log('bower', gutil.colors.cyan(data.id), data.message);
        });
});

gulp.task('git-check', function(done) {
    if (!sh.which('git')) {
        console.log(
            '  ' + gutil.colors.red('Git is not installed.'),
            '\n  Git, the version control system, is required to download Ionic.',
            '\n  Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.',
            '\n  Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
        );
        process.exit(1);
    }
    done();
});
